package April.删除排序链表中的重复元素II;
//4月7日
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
public class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }
class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if(head==null) return head;
        ListNode dummy = new ListNode();
        dummy.next = head;
        ListNode pre = dummy,p = head;
        boolean flag = false;
        while(p.next!=null){
            if(p.val==p.next.val){
                flag=true;
            }else{
                if(flag){
                    pre.next=p.next;
                    flag=false;
                }else
                    pre=p;
            }
            p=p.next;
        }
        if(flag) pre.next=p.next;
        return dummy.next;
    }
}